package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.config.ThreadPoolConflguration;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.mapper.admin.AdminOrderMapper;
import com.sky.mapper.admin.OrderDetaiMapperl;
import com.sky.pojo.OrderDetail;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Intercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class OrderServicelmpl implements OrderService {
    @Resource
    AdminOrderMapper orderMapper;
    @Autowired
    ThreadPoolConflguration threadPoolConflguration;

    @Resource
    OrderDetaiMapperl orderDetaiMapperl;

    @Override
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<OrderVO> orderVOS = orderMapper.conditionSearch(dto);
        PageResult result = PageResult.builder().total(orderVOS.getTotal()).records(orderVOS.getResult()).build();
        return Result.success(result);
    }

    @Override
    public OrderStatisticsVO statistics() {
        //       Integer waiting4MerchantReceiveOrders = orderMapper.statistics(Orders.TO_BE_CONFIRMED);
//        Integer waitng4RiderReceiveOrders = orderMapper.statistics(Orders.CONFIRMED);
//        Integer toBeDeliveryOnProgressOrders = orderMapper.statistics(Orders.TOBEDELIVERED);
//        Integer toBeArrivedOrders = orderMapper.statistics(Orders.TOBEDELIVERED);
//        Integer completedOrders = orderMapper.statistics(Orders.COMPLETE_ORDER);
//        Integer canceledOrders = orderMapper.statistics(Orders.CANCELLED_ORDER);
//
        /**
         *  改成线程池优化
         */


        // 根据状态，   2 用户付款成功，等待商家接单  3.商家接单，等待骑手接单
        //     4 骑手接单成功-待取货  5待送达  6已完成  7已取消
//        long l1 = System.currentTimeMillis();
//        Future<Integer> future1 = threadPoolConflguration.threadPoolExecutor().submit(() -> {
//            return orderMapper.statistics(Orders.TO_BE_CONFIRMED);
//        });
//        Future<Integer> future2 = threadPoolConflguration.threadPoolExecutor().submit(() -> {
//            return orderMapper.statistics(Orders.CONFIRMED);
//        });
//        Future<Integer> future3 = threadPoolConflguration.threadPoolExecutor().submit(() -> {
//            return orderMapper.statistics(Orders.TOBEDELIVERED);
//        });
//        Future<Integer> future4 = threadPoolConflguration.threadPoolExecutor().submit(() -> {
//            return orderMapper.statistics(Orders.TOBEDELIVERED);
//        });
//        Future<Integer> future5 = threadPoolConflguration.threadPoolExecutor().submit(() -> {
//            return orderMapper.statistics(Orders.COMPLETE_ORDER);
//        });
//        Future<Integer> future6 = threadPoolConflguration.threadPoolExecutor().submit(() -> {
//            return orderMapper.statistics(Orders.CANCELLED_ORDER);
//        });
//        Integer waiting4MerchantReceiveOrders = null;
//        Integer waitng4RiderReceiveOrders = null;
//        Integer toBeDeliveryOnProgressOrders = null;
//        Integer toBeArrivedOrders = null;
//        Integer completedOrders = null;
//        Integer canceledOrders = null;
//        try {
//            waiting4MerchantReceiveOrders = future1.get();
//            waitng4RiderReceiveOrders = future2.get();
//            toBeDeliveryOnProgressOrders = future3.get();
//            toBeArrivedOrders = future4.get();
//            completedOrders = future5.get();
//            canceledOrders = future6.get();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        long l2 = System.currentTimeMillis();
//        System.err.println("-------------耗时:" + (l2 - l1));
//        // 将查询出的数据封装到orderStatisticsVO中响应
//        OrderStatisticsVO orderStatisticsVO = OrderStatisticsVO.builder()
//                .waiting4MerchantReceiveOrders(waiting4MerchantReceiveOrders)//  status = 2  等待商家接单
//                .waitng4RiderReceiveOrders(waitng4RiderReceiveOrders)//  status = 3  等待骑手接单
//                .toBeDeliveryOnProgressOrders(toBeDeliveryOnProgressOrders)//  status = 4  待出货订单数
//                .toBeArrivedOrders(toBeArrivedOrders)//  status = 5  待送达订单数
//                .completedOrders(completedOrders) // status = 6  已完成订单数
//                .canceledOrders(canceledOrders)//  status = 7  已取消订单数
//                .build();
//        return orderStatisticsVO;

        /**
         *  优化三 直接使用集合进行处理
         */

        // map.key 是列名，map.value是值
    /*
        返回值格式如下：
        [
            {status:2,total:26},
            {status:3,total:26},
            {status:4,total:26},
            {status:5,total:26},
            {status:7,total:26},
        ]
     */
//        List<Map<String, Object>> mapList = orderMapper.countableStatus();
//        Integer waiting4MerchantReceiveOrders = 0;
//        Integer waitng4RiderReceiveOrders = 0;
//        Integer toBeDeliveryOnProgressOrders = 0;
//        Integer toBeArrivedOrders = 0;
//        Integer completedOrders = 0;
//        Integer canceledOrders = 0;
//
//        for (Map<String, Object> map : mapList) {
//            Integer status = (Integer) map.get("status");
//            Long total = (Long) map.get("a");
//            switch (status) {
//                case 2 -> waiting4MerchantReceiveOrders = total.intValue();
//
//                case 3 -> waitng4RiderReceiveOrders = total.intValue();
//
//                case 4 -> toBeDeliveryOnProgressOrders = total.intValue();
//
//                case 5 -> toBeArrivedOrders = total.intValue();
//
//                case 6 -> completedOrders = total.intValue();
//
//                case 7 -> canceledOrders = total.intValue();
//            }
//        }
//        //构建返回值
//        //        // 将查询出的数据封装到orderStatisticsVO中响应
//        OrderStatisticsVO orderStatisticsVO = OrderStatisticsVO.builder()
//                .waiting4MerchantReceiveOrders(waiting4MerchantReceiveOrders)//  status = 2  等待商家接单
//                .waitng4RiderReceiveOrders(waitng4RiderReceiveOrders)//  status = 3  等待骑手接单
//                .toBeDeliveryOnProgressOrders(toBeDeliveryOnProgressOrders)//  status = 4  待出货订单数
//                .toBeArrivedOrders(toBeArrivedOrders)//  status = 5  待送达订单数
//                .completedOrders(completedOrders) // status = 6  已完成订单数
//                .canceledOrders(canceledOrders)//  status = 7  已取消订单数
//                .build();
//        return orderStatisticsVO;
        /**
         * 优化方案4
         * 直接对sql语句进行优化 使用行列转换
         */
        OrderStatisticsVO orderStatisticsVO = orderMapper.statisticsAllStatusOrderCount2();
        return orderStatisticsVO;
    }

    @Override
    public OrderVO adminOrderDetails(Long id) {
        OrderVO orderVO = orderMapper.adminOrderDetails(id);
        List<OrderDetail> byIdDetai = orderDetaiMapperl.getByIdDetai(id);
        log.warn("商家查询订单的数据{}",orderVO);
        orderVO.setOrderDetailList(byIdDetai);
        return orderVO;
    }
}

